package com.swt.testdemo.controller;

import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.config.ResponseMessage;
import com.swt.testdemo.form.CreateDataTemplateForm;
import com.swt.testdemo.form.EditDataTemplateForm;
import com.swt.testdemo.form.QueryDataSourceForm;
import com.swt.testdemo.form.QueryDataTemplateForm;
import com.swt.testdemo.service.SysDataSourceService;
import com.swt.testdemo.service.SysDataTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/12 17:14
 **/
@RestController
@RequestMapping("/sys/data")
@Api(tags = "后台管理--数据管理")
@Slf4j
public class SysDataController {


    @Autowired
    private SysDataTemplateService sysDataTemplateService;

    @Autowired
    private SysDataSourceService sysDataSourceService;


    @ApiOperation("01 查看数据模板列表")
    @GetMapping(value = "/template-list")
    public ResponseEntity<ResponseBean> listDataTemplate(QueryDataTemplateForm form){
        ResponseBean response = sysDataTemplateService.listDataTemplate(form);
        return ResponseEntity.status(response.getStatus()).body(response);

    }



    @ApiOperation("02 查看模板信息")
    @GetMapping(value = "/template-info")
    @ApiImplicitParam(name = "dataTemplateId",value = "模板id",paramType = "query",dataType = "int",required = true,defaultValue = "197")
    public ResponseEntity<ResponseBean> getDataTemplateInfo(Integer dataTemplateId){
        ResponseBean response = sysDataTemplateService.getDataTemplateInfo(dataTemplateId);
        return ResponseEntity.status(response.getStatus()).body(response);

    }

    @ApiOperation(value = "03 下载模板",produces = "application/octet-stream")
    @GetMapping(value = "/template-download")
    @ApiImplicitParam(name = "dataTemplateId",value = "模板id",paramType = "query",dataType = "int",required = true,defaultValue = "1")
    public ResponseEntity<ResponseMessage> downloadDataTemplate(HttpServletRequest request, HttpServletResponse response, Integer dataTemplateId){
        ResponseMessage message = sysDataTemplateService.downloadDataTemplate(request, response, dataTemplateId);
        return ResponseEntity.status(message.getStatus()).body(message);

    }

    @ApiOperation(value = "04 创建模板")
    @PostMapping(value = "/template-create")
    public ResponseEntity<ResponseBean> createDataTemplate(@RequestBody CreateDataTemplateForm form){
        ResponseBean response = sysDataTemplateService.createDataTemplate(form);
        return ResponseEntity.status(response.getStatus()).body(response);

    }

    @ApiOperation(value = "05 编辑模板")
    @PutMapping(value = "/template-edit")
    public ResponseEntity<ResponseBean> editDataTemplate(@RequestBody EditDataTemplateForm form){
        ResponseBean response = sysDataTemplateService.editDataTemplate(form);
        return ResponseEntity.status(response.getStatus()).body(response);

    }

    @ApiOperation("06 删除模板")
    @DeleteMapping(value = "/template-delete")
    @ApiImplicitParam(name = "dataTemplateId",value = "模板id",paramType = "query",dataType = "int",required = true,defaultValue = "1")
    public ResponseEntity<ResponseMessage> deleteDataTemplate(Integer dataTemplateId){
        ResponseMessage response = sysDataTemplateService.deleteDataTemplate(dataTemplateId);
        return ResponseEntity.status(response.getStatus()).body(response);

    }

    @ApiOperation("07 获取数据列表")
    @GetMapping(value = "/list")
    public ResponseEntity<ResponseBean> getDataSourceList(QueryDataSourceForm form){
        ResponseBean response = sysDataTemplateService.getDataSourceList(form);
        return ResponseEntity.status(response.getStatus()).body(response);

    }




}
